package com.cosc716.restaurant.domain.collections;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;

import com.cosc716.restaurant.domain.Customer;
import com.cosc716.restaurant.domain.Order;

public class OrderQueue extends Observable {

	private ArrayList<Order> orders;
	
	public OrderQueue() {
		orders = new ArrayList<Order>();
	}
	
	public boolean addOrder(Order order) {
		if (orders.add(order)) {
			setChanged();
			super.notifyObservers();
			return true;
		}
		return false;
	}
	
	public boolean removeOrder(Order order) {
		if (orders.remove(order)) {
			setChanged();
			super.notifyObservers();
			return true;
		}
		return false;
	}
	
	public ArrayList<Order> getOrders() {
		return orders;
	}
	
	public int size() {
		return orders.size();
	}
	
	public void clearOrders() {
		this.orders.clear();
		setChanged();
		super.notifyObservers();
	}
	
	public static CustomerOrderIterator
		getCustomerOrderIterator(OrderQueue orders, Customer customer) {
		return new CustomerOrderIterator(orders, customer);
	}
	
	public CustomerOrderIterator getCustomerOrderIterator(Customer customer) {
		return getCustomerOrderIterator(this, customer);
	}
	
	public static OrderIterator getOrderIterator(OrderQueue orders) {
		return new OrderIterator(orders);
	}
	
	public OrderIterator getOrderIterator() {
		return getOrderIterator(this);
	}
	
	
}

class OrderIterator implements Iterator<Order> {

	private ArrayList<Order> orders = null;
	private int position = 0;
	
	public OrderIterator(OrderQueue orders) {
		this.orders = orders.getOrders();
	}
	@Override
	public boolean hasNext() {
		while(position < orders.size()) {
			return true;
		}
		
		return false;
	}

	@Override
	public Order next() {
		return orders.get(position++);
	}

	@Override
	public void remove() {
		orders.remove(position);
	}
	
}

class CustomerOrderIterator implements Iterator<Order> {

	private ArrayList<Order> orders = null;
	private int position = 0;
	private Customer customer = null;
	
	public CustomerOrderIterator(OrderQueue orders, Customer customer) {
		this.orders = orders.getOrders();
		this.customer = customer;
	}

	@Override
	
	public boolean hasNext() {
		/**while(position < orders.size()) {
			if(orders.get(position).getCustomer().getCustomerId().
					equals(customer.getCustomerId())) {
				return true;
			}
			position++;
		}
		**/
		
		return false;
	}

	@Override
	public Order next() {
		return orders.get(position++);
	}

	@Override
	public void remove() {
		orders.remove(position);
	}
	
}
